package com.richard.face.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.richard.face.entity.UserInfoEntity;
import com.richard.face.service.UserInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserInfoController {
    // 登录功能
    @PostMapping("/login")
    public Map enter(@RequestBody Map<String, String> param) {
        Map result = new HashMap<String, Object>();
        String user = param.get("user");
        String password = param.get("password");
        List<UserInfoEntity> list = UserInfoService.userInfoService.login(user, password);
        System.out.println(list.size());
        if (list.size() >= 1) {
            result.put("status", 1);
            result.put("msg", "登录成功");
            result.put("user", list.get(0).getUser());
            result.put("gender", list.get(0).getPassword());
        } else {
            result.put("status", 0);
            result.put("msg", "登录失败");
        }
        return result;
    }

    // 注册功能
    @PostMapping("/registration")
    public Map registration(@RequestBody Map<String, String> param) {
        Map result = new HashMap<String, Object>();
        String user = param.get("user");
        String password = param.get("password");
        var step = UserInfoService.userInfoService.insert(user, password);
        if (step == 1) {
            result.put("status", 1);
            result.put("msg", "注册成功");
        } else {
            result.put("status", 0);
            result.put("msg", "注册失败");
        }
        return result;
    }

    // 密码更改
    @PostMapping("/password_reset")
    public Map<String, Object> passwordReset(@RequestBody Map<String, String> param) {
        Map<String, Object> result = new HashMap<>();
        String user = param.get("username");
        String password = param.get("oldPassword");
        String newPassword = param.get("newPassword");
        String confirmPassword = param.get("confirmPassword");

        System.out.println(user + " " + password + " " + newPassword + " " + confirmPassword);

        // 确保所有必需参数都存在
        if (user == null || password == null || newPassword == null || confirmPassword == null) {
            result.put("status", -1);
            result.put("msg", "缺少必要参数");
            return result;
        }

        // 检查新密码和确认密码是否匹配
        if (!newPassword.equals(confirmPassword)) {
            result.put("status", 0);
            result.put("msg", "新密码和确认密码不匹配");
            return result;
        }

        try {
            // 假设UserLoginService是正确实现的Service层，用于处理用户登录和密码重置逻辑
            List<UserInfoEntity> loginResult = UserInfoService.userInfoService.login(user, password);

            if (loginResult != null && !loginResult.isEmpty()) {
                // 实际应用中，应确保只有一条记录匹配，此处简化处理
                UserInfoService.userInfoService.password_reset(user, newPassword);
                result.put("status", 1);
                result.put("msg", "密码重置成功");
            } else {
                result.put("status", 0);
                result.put("msg", "验证失败，请检查用户名和密码是否正确");
            }
        } catch (Exception e) {
            // 记录异常信息，实际应用中应使用日志框架
            e.printStackTrace();
            result.put("status", 2);
            result.put("msg", "密码重置过程中发生错误");
        }

        return result;
    }

    // 更新用户信息
    @PostMapping("/UserProfile")
    public Map<String, Object> update_user_Profile(@RequestBody Map<String, String> param) {
        Map<String, Object> result = new HashMap<>();

        String username = param.get("username"); // 假设前端传递的是"username"
        String gender = param.get("gender");
        String age = param.get("age");
        String classId = param.get("classId");
        String stuId = param.get("stuId");
        String professional = param.get("professional");
        String nickname = param.get("nickname");

        if (username != null && !username.isEmpty()) {
            UserInfoService.userInfoService.update_user_profile(username, gender, age, classId, stuId, professional, nickname);
            System.out.println(username);
            result.put("status", 1);
            result.put("msg", "更新成功");
        } else {
            result.put("status", 0);
            result.put("msg", "更新失败：用户名不能为空");
        }
        return result;
    }

    // 个人信息页面


    // 查询个人信息
    @PostMapping("/UserProfile_select")
    public Map<String, Object> select_user_profile(@RequestBody Map<String, String> param) {
        Map<String, Object> result = new HashMap<>();
        String user = param.get("user");
        String gender = param.get("gender");
        String age = param.get("age");
        String classId = param.get("classId");
        String stuId = param.get("stuId");
        String professional = param.get("professional");
        String nickname = param.get("nickname");

        List<UserInfoEntity> list = UserInfoService.userInfoService.select_user_profile(user, gender, age, classId, stuId, professional, nickname);
        result.put("userinfo", list);
        if (list.size() >= 1) {
            result.put("status", 1);
            result.put("msg", "查询成功");
        } else {
            result.put("status", 0);
            result.put("msg", "查询失败");
        }
        return result;
    }

    // 查询所有数据
//    @PostMapping("/UserProfile_select_all")
//    public Map<String, Object> select_user_profile_all(@RequestBody Map<String, String> param) {
//        Map<String, Object> result = new HashMap<>();
//        String id = param.get("id");
//        String user = param.get("user");
//        String gender = param.get("gender");
//        String age = param.get("age");
//        String classId = param.get("classId");
//        String stuId = param.get("stuId");
//        String professional = param.get("professional");
//        String nickname = param.get("nickname");
//        List<UserInfoEntity> list = UserInfoService.userInfoService.select_all_user_profile(id, user, gender, age, classId, stuId, professional, nickname);
//        result.put("userinfo", list);
//
//        return result;
//    }

    // 删除某条数据？？？？？？？
    @PostMapping("/UserProfile_delete")
    public Map<String, Object> deleteUserProfile(@RequestBody Map<String, String> param) {
        Map<String, Object> result = new HashMap<>();
        System.out.println(param.get("id"));
        int isDeleted = UserInfoService.userInfoService.delete_user_profile(param.get("id"));

        if (isDeleted == 1) {
            result.put("status", 200);
            result.put("msg", "删除成功");
        } else {
            result.put("status", 0);
            result.put("msg", "删除失败");
        }

        return result;
    }

    // 查询某类数据
    @PostMapping("/select_user")
    public Map<String, Object> select_user(@RequestBody Map<String, String> param) {
        Map<String, Object> result = new HashMap<>();
        var user = param.get("user");
        var gender = param.get("gender");
        var age = param.get("age");
        var classId = param.get("classId");
        var stuId = param.get("stuId");
        var professional = param.get("professional");
        var nickname = param.get("nickname");
        System.out.println(user + gender + age + classId + stuId + professional + nickname);
        List<UserInfoEntity> list = UserInfoService.userInfoService.select_user(user, gender, age, classId, stuId, professional, nickname);

        result.put("userinfo", list);

        return result;
    }

    // 插入新用户(学生信息管理界面)
    @PostMapping("/insert_new_user")
    public Map<String, Object> insert_new_msg(@RequestBody Map<String, String> param) {
        Map<String, Object> result = new HashMap<>();

        try {
            String user = param.get("user");
            String password = param.get("password");
            String gender = param.get("gender");
            String age = param.get("age");
            String classId = param.get("classId");
            String stuId = param.get("stuId");
            String professional = param.get("professional");
            String nickname = param.get("nickname");
            System.out.println(user + password + gender + age + classId + stuId + professional + nickname);
            int isInserted = UserInfoService.userInfoService.insert_new_msg(user, password, gender, age, classId, stuId, professional, nickname);
            if (isInserted == 1) {
                result.put("status", 1);
                result.put("msg", "插入成功");
                System.out.println(result);
            } else {
                result.put("status", 0);
                result.put("msg", "插入失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
    // 登录前重置密码（需要账号和与原密码）
}